-- @name 多地图循环任务
-- @version 1.0.0
-- @description 自动遍历多个地图并执行停留操作
-- @require modules/map.lua
-- @require modules/movement.lua

local logger = require("utils.logger")
local ConfigLoader = require("config.loader")
local Settings = require("config.settings")
local config = ConfigLoader.load(Settings.CURRENT_GAME)
local map = require("modules.map")
local movement = require("modules.movement")
local combat = require("modules.combat")
local window = require("modules.window")


-- 玛雅层级检查函数
local function checkMayaLevel(level)
    local colorConfig = {
        [2] = config.MOVEMENT.MAYA_NAVIGATION.LEVEL2_COLORS,
        [3] = config.MOVEMENT.MAYA_NAVIGATION.LEVEL3_COLORS,
        ['hall'] = config.MOVEMENT.MAYA_NAVIGATION.HALL_COLORS
    }
    return multiColor(colorConfig[level])
end

-- 通用进图重试机制
local function enterMayaLevelWithRetry(level, coord)
    local retry_count = 0
    local max_retry = 10
    while retry_count < max_retry do
        map.findBsAndGoto(coord[1], coord[2])
        movement.dragDirection('northwest', level == 'hall' and 2500 or 1500)

        if checkMayaLevel(level) then
            logger.info('成功进入玛雅' .. tostring(level))
            return true
        end

        retry_count = retry_count + 1
        logger.warn("进入失败，等待重试（第" .. retry_count .. "次）")
        mSleep(2000)
    end
    return false
end



-- -- 修改后的进图调用方式
-- enterMayaLevelWithRetry(2, {495, 302})
-- enterMayaLevelWithRetry(3, {495, 302})
-- enterMayaLevelWithRetry('hall', {485, 320})


local function main()
    logger.info("启动玛雅地图任务")
    local map_name = '玛雅神殿'
    while true do
        if movement.isInMZ() then
            local success = false
            local retry_count = 0

            -- 带重试机制的进图逻辑
            while not success and retry_count < 3 do
                logger.info(string.format("尝试进入%s（第%d次）", map_name, retry_count + 1))
                -- 进入地图
                combat.stopAutoAttack()
                window.closeOtherWindow()
                if map.enterMapByPoint(map_name) then
                    logger.info(string.format("成功进入%s", map_name))
                    -- 修改后的层级检查逻辑
                    if checkMayaLevel(2) then
                        logger.info("成功进入2层")
                        enterMayaLevelWithRetry(3, { 495, 302 })
                    elseif checkMayaLevel(3) then
                        logger.info("成功进入3层")
                        success = true
                    elseif checkMayaLevel('hall') then
                        logger.info("成功进入大厅")
                    else
                        logger.info("成功进入1层")
                        enterMayaLevelWithRetry(2, { 495, 302 })
                    end
                else
                    retry_count = retry_count + 1
                    logger.warn("进入地图失败，等待重试")
                    mSleep(5000)
                end
            end

            if not success then
                logger.error(string.format("%s进入失败超过最大重试次数", map_name))
                mSleep(5 * 1000)
            else
                combat.startAutoAttack()
                mSleep(15 * 60 * 1000)
            end
        end
    end
end

return {
    run = main
}
